package com.shuma.shopmanage.usermoudle.service.Impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuma.shopmanage.common.helper.JwtHelper;
import com.shuma.shopmanage.common.result.Result;

import com.shuma.shopmanage.model.user.dto.UserDto;
import com.shuma.shopmanage.model.user.model.Role;
import com.shuma.shopmanage.model.user.model.UserInfo;
import com.shuma.shopmanage.model.user.vo.LoginVo;
import com.shuma.shopmanage.usermoudle.mapper.RoleMapper;
import com.shuma.shopmanage.usermoudle.mapper.UserMapper;
import com.shuma.shopmanage.usermoudle.service.USerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserInfo> implements USerService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserMapper userMapper;
    @Override

    public Result login(UserDto dto) {
        //1.正常用户登录 用户名和密码
        if(StringUtils.isNotBlank(dto.getUsername()) && StringUtils.isNotBlank(dto.getPassword())) {
            //1.1 根据用户名查询用户信息
            UserInfo dbUser = getOne(Wrappers.<UserInfo>lambdaQuery().eq(UserInfo::getUsername,dto.getUsername()));
            if(dbUser == null){
                return Result.fail("用户信息不存在");
            }

            Role dbRole = roleMapper.getRoleById(dbUser.getRoleId());
            //1.2 比对密码
//            String salt = dbUser.getSalt();
            String password = dto.getPassword();
//            String pswd = DigestUtils.md5Hex((password+salt).getBytes());
            if(!password.equals(dbUser.getPassword())){
                return Result.fail("账号或密码错误");
            }

            //1.3 返回数据 jwt
           // String token = JwtHelper.createToken(dbUser.getId().longValue(),dbUser.getUsername(),dbUser.getRoleId());
            String token = JwtHelper.createToken(dbUser.getId().longValue(),dbUser.getUsername(), dbRole.getName());
            LoginVo loginVo =LoginVo.builder()
                    .id(dbUser.getId())
                    .token(token)
                    .role(dbRole.getName())
                    .build();
            return Result.ok(loginVo);
        }else {
            //2.游客登录
            Map<String,Object> map = new HashMap<>();
            //map.put("token",JwtHelper.getToken(0L));
            return Result.ok(map);
        }
    }

    @Override
    public String selectNameById(Long id) {
        UserInfo userInfo = query().eq("id", id).one();
        if (ObjectUtils.isEmpty(userInfo)) {
            return null;
        }
        return userInfo.getUsername();
    }

    @Override
    public List<Long> selectIdByLikeName(String name) {
        List<UserInfo> users = query().like("username", "%" + name + "%")
                .list();
        List<Long> list = users.stream().map(UserInfo::getId).collect(Collectors.toList());
        return list;
    }

    @Override
    public Long selectIdByName(String name) {
        Long userId = userMapper.getIdByName(name);
        return userId;
    }
}
